<template>
  <div>
    <template-table :temInput="temInput" :temTable="temTable" :url="selUrl" @getData="getData" @btnClick="btnClick"
      @handleSelectionChange="handleSelectionChange" :temTab="temTab" @inputBtnClick="inputBtnClick"
      @tableBtnClick="tabBtn" @selChange="selChange" ref="child"></template-table>
    <input type="file" @change="importfxx(this)" ref="xlsxFile"
      accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
      style="display: none" />

    <el-dialog title="查看详情" width="50%" :visible.sync="dialogTableVisible" @close="closed">
      <el-row>
        <el-col :span="24">
          <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
            <el-tab-pane label="查看详情" name="first"></el-tab-pane>
            <div v-if="activeName == 'first'">
              <ul>
                <li>
                  <span>头像：</span>
                  <img :src="detailData.userImgurl" alt="暂无" />
                </li>
                <li>
                  <span>登录名：</span>
                  <span>{{ detailData.employeePhone }}</span>
                </li>
                <li>
                  <span>真实姓名：</span>
                  <span>{{ detailData.employeeName }}</span>
                </li>
                <li>
                  <span>手机号：</span>
                  <span>{{ detailData.employeePhone }}</span>
                </li>
                <li>
                  <span>昵称：</span>
                  <span>{{ detailData.userNicknam }}</span>
                </li>
                <li>
                  <span>生日：</span>
                  <span>{{ detailData.userBirthday }}</span>
                </li>
                <li>
                  <span>性别：</span>
                  <span>{{ detailData.userSex }}</span>
                </li>
              </ul>
            </div>

            <el-tab-pane label="查看部门" name="second"></el-tab-pane>
            <div v-if="activeName == 'second'">
              <el-card class="box-card">
                <div v-for="(departName, index) in dapartdata" :key="index" class="text item">
                  {{ departName.departName }}
                </div>
              </el-card>
            </div>

            <el-tab-pane label="公司职位信息" name="third"> </el-tab-pane>
            <div v-if="activeName == 'third'">
              <el-table :data="tableData" border style="width: 100%">
                <el-table-column prop="companyShortname" label="公司名称"> </el-table-column>
                <el-table-column prop="positionName" label="公司职位"> </el-table-column>
                <el-table-column prop="dataState" label="状态"> </el-table-column>
              </el-table>
            </div>

            <el-tab-pane label="部门职位信息" name="fourth"> </el-tab-pane>
            <div v-if="activeName == 'fourth'">
              <el-table :data="dapartdata1" border style="width: 100%">
                <el-table-column prop="departName" label="部门名称"> </el-table-column>
                <el-table-column prop="positionName" label="部门职位"> </el-table-column>
                <el-table-column prop="dataState" label="状态"> </el-table-column>
              </el-table>
            </div>

            <el-tab-pane label="群组职位信息" name="fifth"> </el-tab-pane>
            <div v-if="activeName == 'fifth'">
              <el-table :data="groupdata" border style="width: 100%">
                <el-table-column prop="groupName" label="群组名称"> </el-table-column>
                <el-table-column prop="positionName" label="群组职位"> </el-table-column>
                <el-table-column prop="dataState" label="状态"> </el-table-column>
              </el-table>
            </div>


            <el-tab-pane label="查看群组" name="sixth"> </el-tab-pane>
            <div v-if="activeName == 'sixth'">
              <el-card class="box-card">
                <div v-for="(groupName, index) in groupdata" :key="index" class="text item">
                  {{ groupName.groupName }}
                </div>
              </el-card>
            </div>
          </el-tabs>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer"></div>
    </el-dialog>
    <el-dialog title="分配部门" width="35%" :visible.sync="dialogTableVisible1">
      <el-form>
        <el-form-item label="分配部门：" :label-width="formLabelWidth">
          <el-select v-model="value5" multiple placeholder="请选择">
            <el-option v-for="item in options" :key="item.departCode" :label="item.departName" :value="item.departCode">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="sureright">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="分配群组" width="35%" :visible.sync="dialogTableVisible2">
      <el-form>
        <el-form-item label="分配群组：" :label-width="formLabelWidth">
          <el-select v-model="value6" multiple placeholder="请选择">
            <el-option v-for="item in options1" :key="item.groupCode" :label="item.groupName" :value="item.groupCode">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="surerightone">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog title="编辑员工" :visible.sync="editVisible">
      <el-form :model="editForm" ref="editForm" :rules="rules">
        <el-form-item label="所属企业" :label-width="formLabelWidth" prop="companyCode">
          <el-select disabled="true" v-model="editForm.companyCode" placeholder="请选择所属企业" clearable filterable>
            <el-option v-for="item in optionsCompany" :key="item.companyCode" :label="item.companyName"
              :value="item.companyCode"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="员工姓名" :label-width="formLabelWidth" prop="employeeName">
          <el-input v-model="editForm.employeeName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth" prop="employeePhone">
          <el-input v-model="editForm.employeePhone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSure(editForm)">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分配 样式1标品的 -->
    <el-dialog title="分配" :visible.sync="alllocationVisible" @close="closefenpei">
      <el-form :model="alllocationForm" ref="alllocationForm">
        <el-form-item label="所属企业" :label-width="formLabelWidth">
          <el-select disabled="true" v-model="alllocationForm.companyCode" placeholder="请选择所属企业" clearable filterable>
            <el-option v-for="item in optionsCompany" :key="item.companyCode" :label="item.companyName"
              :value="item.companyCode"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择公司职位" :label-width="formLabelWidth">
          <el-select v-model="alllocationForm.companyPositionCode" placeholder="选择职位" filterable clearable
            @visible-change="getEmployees1(alllocationForm, 1, $event)">
            <el-option v-for="item in optionParent1" :key="item.positionCode" :label="item.positionName"
              :value="item.positionCode"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择部门名称" :label-width="formLabelWidth">
          <el-select v-model="alllocationForm.departCode" placeh filterable clearable
            @visible-change="getEmployees1(alllocationForm, 2, $event)" @clear="clearAlldepart()">
            <el-option v-for="item in optionParent2" :key="item.departCode" :label="item.departName"
              :value="item.departCode"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择部门职位" :label-width="formLabelWidth">
          <el-select v-model="alllocationForm.departPositionCode" placeholder="选择职位" filterable clearable
            @visible-change="getEmployees1(alllocationForm, 3, $event)">
            <el-option v-for="item in optionParent3" :key="item.positionCode" :label="item.positionName"
              :value="item.positionCode"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择群组名称" :label-width="formLabelWidth">
          <el-select v-model="alllocationForm.groupCode" placeholder="选择名称" filterable clearable
            @visible-change="getEmployees1(alllocationForm, 4, $event)" @clear="clearAllgroup()">
            <el-option v-for="item in optionParent4" :key="item.groupCode" :label="item.groupName"
              :value="item.groupCode"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择群组职位" :label-width="formLabelWidth">
          <el-select v-model="alllocationForm.groupPositionCode" placeholder="选择职位" filterable clearable
            @visible-change="getEmployees1(alllocationForm, 5, $event)">
            <el-option v-for="item in optionParent5" :key="item.positionCode" :label="item.positionName"
              :value="item.positionCode"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="alllocationVisible = false">取 消</el-button>
        <el-button type="primary" @click="alllocationSure(alllocationForm)">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 666， 分配 分配要样式2的 -->
    <el-dialog title="分配管理组织" :visible.sync="assignmentStyle2" @close="closeAssignmentStyle2">
      <!-- 联级选择器 -->
      <el-cascader style="width: 80%;margin-left: 10%;" v-model="cascaderValue" :options="cascaderOptions"
        :props="{ expandTrigger: 'hover' }" @change="handleCascaderChange"></el-cascader>


      <div slot="footer" class="dialog-footer">
        <el-button @click="assignmentStyle2 = false">取 消</el-button>
        <el-button type="primary" @click="assignmentStyle2Submit()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 666， 分配 分配要样式3的 ：原型换了，返工 呜呜呜-->
    <el-dialog title="分配管理组织" width="80%" :visible.sync="assignmentStyle3" @close="closeAssignmentStyle3">
      <!-- 分配管理组织树形选择器 -->
      <manageOrganization @selectRowChange="selectRowChange"></manageOrganization>

      <div slot="footer" class="dialog-footer">
        <el-button @click="assignmentStyle3 = false">取 消</el-button>
        <el-button type="primary" @click="assignmentStyle3Submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import manageOrganization from './component/manageOrganization.vue' //引入组件
import API from '@/api/api.js'
import {
  searchcompany,
  searchoptionlist,
  searchdepartment,
  searchgroup,
  updatedepart,
  updategroup,
  searchmemberdetail,
  queryCompanyPageByYG,
  queryDepartPageByYG,
  queryGroupPageByYG,
  queryUserservicePageByYG,
  queryUserserviceCurrency,
  getCompany,
  updateEmployee,
  saveEmployeePlusBatch,
  queryPositionPage,
  updateEmployeeToCompany,
} from '@/api/organization.js'
import templateTable from '#/views/templates/templateTable'
import { parseTime } from '@/utils/index'
const querystring = require('querystring')
export default {
  components: {
    'template-table': templateTable,
    manageOrganization,
  },
  data() {
    return {
      selectedRows: [], //选中的组织行
      assignmentStyle3: false,
      multipleSelection: [], // 选中的行
      cascaderValue: [],
      cascaderOptions: [],
      assignmentStyle2: false,
      rules: {
        companyCode: [
          {
            required: true,
            message: '请选择所属企业',
            trigger: 'blur',
          },
        ],
        employeeName: [
          {
            required: true,
            message: '请输入员工姓名',
            trigger: 'blur',
          },
          // {
          //   pattern: /^\w{2,16}$/,
          //   message: "请输入2-16位数字或中英文字符",
          //   trigger: "blur"
          // }
        ],
        employeePhone: [
          {
            required: true,
            message: '请输入手机号',
            trigger: 'blur',
          },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur',
          },
        ],
      },
      optionParent1: [],
      optionParent2: [],
      optionParent3: [],
      optionParent4: [],
      optionParent5: [],
      editVisible: false,
      editForm: '',
      alllocationVisible: false,
      alllocationForm: {},
      optionsCompany: '',
      zdpartcode: [],
      formLabelWidth: '120px',
      sdpartcode: [],
      bdpartcode: [],
      options: [],
      options1: [],
      tableData: [],
      value: '',
      value5: [],
      value6: [],
      parseTime,
      detailinfo: {},
      temInput: [
        {
          label: '公司名称',
          name: 'companyCode',
          type: 'select',
          props: {
            label: 'companyName',
            value: 'companyCode',
          },
          pagenation: {
            page: 1,
            rows: 10000,
          },
          options: [],
          filterable: true,
          url: '/web/org/company/queryCompanyByGS.json',
          placeholder: '请选择公司名称',
          value: [],
        },
        // {
        // 	label: "公司名称",
        // 	type: "text",
        // 	name: "companyShortname",
        // 	value: "",
        // 	placeholder: "请输入公司简称",
        // },
        {
          label: '员工名称',
          type: 'text',
          name: 'employeeName',
          value: '',
          placeholder: '请输入员工名称',
        },
        {
          label: '手机号',
          type: 'text',
          name: 'employeePhone',
          value: '',
          placeholder: '请输入手机号',
        },
        {
          label: "状态",
          type: "select",
          name: "dataState",
          value: "",
          placeholder: "请选择状态",
          options: [
            {
              label: "全部",
              value: "0,-1",
            },
            {
              label: "启用",
              value: "0",
            },
            {
              label: "禁用",
              value: "-1",
            },
          ],
        },
        {
          btnArray: [
            {
              label: '查询',
              id: 'query',
              type: 'primary',
            },

            // {
            //   label: '员工导入',
            //   id: 'insert', //表示查询
            //   type: 'primary',
            // },
            // {
            //   label: '下载模板',
            //   id: 'loaddown', //表示查询
            //   type: 'primary',
            // }, 
            //          {
            //            label: "新增",
            //            id: "add",
            //            type: "primary",
            //            template: {
            //              type: "json",
            //              title: "新增员工",
            //              labelWidth: "8rem",
            //              html: [
            //                {
            //                  label: "所属企业",
            //                  type: "select",
            //                  state: true,
            //                  id: "selCode",
            //                  name: "companyCode",
            //                  value: [],
            //                  props: {
            //                    label: "companyName",
            //                    value: "companyCode"
            //                  },
            //                  placeholder: "请选择所属企业",
            //                  options: []
            //                },
            //                {
            //                  name: "userinfoCode",
            //                  value: ""
            //                },
            //                {
            //                  name: "companyShortname",
            //                  value: ""
            //                },
            //                {
            //                  label: "员工名称",
            //                  type: "text",
            //                  state: "2",
            //                  name: "employeeName",
            //                  placeholder: "输入员工名称",
            //                  value: ""
            //                },
            //                {
            //
            //
            //                  name: "employeePhone",
            //
            //                  value: ""
            //                },
            //                {
            //                  label: "关联操作员",
            //                  type: "select",
            //                   id: "userPCode",
            //                  state: true,
            //                  name: "userCode",
            //                  filterable: true,
            //                  value: [],
            //                  props: {
            //                    label: "userPhone",
            //                    value: "userCode"
            //                  },
            //                  placeholder: "请选择关联操作员",
            //                  options: []
            //                },
            //                {
            //                  btnArray: [
            //                    {
            //                      type: "primary",
            //                      id: "confirm",
            //                      url: "/web/org/employee/saveEmployee.json",
            //                      label: "保存"
            //                    },
            //                    {
            //                      label: "取消",
            //                      id: "cancel"
            //                    }
            //                  ]
            //                }
            //              ],
            //              rules: {
            //                companyCode: [
            //                  {
            //                    required: true,
            //                    message: "请选择所属企业",
            //                    trigger: "blur"
            //                  }
            //                ],
            //                employeeName: [
            //                  {
            //                    required: true,
            //                    message: "请输入员工名称",
            //                    trigger: "blur"
            //                  }
            //                  // {
            //                  //   pattern: /^\w{2,16}$/,
            //                  //   message: "请输入2-16位数字或中英文字符",
            //                  //   trigger: "blur"
            //                  // }
            //                ],
            //                userCode: [
            //                  {
            //                    required: true,
            //                    message: "请输入关联类型",
            //                    trigger: "blur"
            //                  }
            //                ]
            //              }
            //            }
            //          }
          ],
        },
      ],
      temTable: {
        selection: {
          state: true,
          params: [], //单选的参数
        },
        tableHead: [
          {
            prop: 'employeeName',
            label: '真实姓名',
          },
          {
            prop: 'employeePhone',
            label: '手机号',
          },
          {
            prop: '',
            label: '登录ID',
          },
          {
            prop: 'positionName',
            label: '职位名称',
          },
          {
            prop: '',
            label: 'EHR管理组织',
          },
          {
            prop: '',
            label: '角色',
          },
          {
            prop: "dataState",
            label: "状态",
            template: {
              '0': "启用",
              '-1': "禁用",
            },
          },
          // {
          //   prop: 'groupPositionName',
          //   label: '群组职位',
          // }, 
          // {
          //   prop: 'employeeAddress',
          //   label: '内购金额',
          //   renderTemp:this.renderTemp2
          // },
          // {
          //   prop: 'employeeName',
          //   label: '内购余额',
          //   renderTemp:this.renderTemp1
          // },
        ],
        tableData: [],
        tableBtn: [
          {
            label: '查看',
            state: true,
            // dataState: '0',
            id: "edit22",
          },
          // {
          //   label: "启用",
          //   state: false,
          //   prompt: "true", // 可选参数，为false不显示提示框
          //   promptTitle: "是否启用该账号？",
          //   params: {
          //     // 单单只对停用启用有效
          //     userinfoId: "", // 当前条数据code/id
          //   },
          //   dataState: "-1", // 状态参数
          //   id: "open",
          //   url: "/web/um/userDealer/enableUserDealerByUser.json",
          // },

          // {
          //   label: "禁用",
          //   state: false,
          //   promptTitle: "是否禁用该账号？",
          //   prompt: "true", // 可选参数，为false不显示提示框
          //   params: {
          //     // 单单只对停用启用有效
          //     userinfoId: "", // 当前条数据code/id
          //   },
          //   props: {
          //     dataState: "dataState",
          //   },
          //   dataState: "0", // 状态参数
          //   url: "/web/um/userDealer/stoppedUserDealerByUser.json",
          //   id: "stop", // 表示重置类型是一样的
          // },
          // {
          //   label: '分配',
          //   state: true,
          //   // dataState: "0",
          //   id: 'edit222',
          //   color: 'red',
          // },
          // {
          //   label: '分配',
          //   state: true,
          //   id: 'edit333',
          // },
          {
            label: '分配',
            state: true,
            id: 'edit444',
          },
          {
            label: '编辑',
            state: true,
            id: 'edit1',
            template: {
              type: 'json',
              title: '编辑员工',
              labelWidth: '8rem',
              html: [
                {
                  label: '所属企业',
                  type: 'select',
                  name: 'companyCode',
                  // disabled: true,
                  value: '',
                  props: {
                    label: 'companyName',
                    value: 'companyCode',
                  },
                  placeholder: '请选择所属企业',
                  options: [],
                },
                {
                  label: '员工姓名',
                  type: 'text',
                  name: 'employeeName',
                  placeholder: '输入员工姓名',
                  value: '',
                },
                {
                  label: '手机号',
                  type: 'text',
                  name: 'employeePhone',
                  placeholder: '输入手机号',
                  value: '',
                },
                {
                  name: 'employeeId',
                  value: '',
                },
                {
                  name: 'employeeCode',
                  value: '',
                },

                {
                  btnArray: [
                    {
                      type: 'primary',
                      id: 'confirm',
                      url: '/web/org/employee/updateEmployee.json',
                      label: '保存',
                    },
                    {
                      label: '取消',
                      id: 'cancel',
                    },
                  ],
                },
              ],
              rules: {
                companyCode: [
                  {
                    required: true,
                    message: '请选择所属企业',
                    trigger: 'blur',
                  },
                ],
                employeeName: [
                  {
                    required: true,
                    message: '请输入员工姓名',
                    trigger: 'blur',
                  },
                  // {
                  //   pattern: /^\w{2,16}$/,
                  //   message: "请输入2-16位数字或中英文字符",
                  //   trigger: "blur"
                  // }
                ],
                employeePhone: [
                  {
                    required: true,
                    message: '请输入手机号',
                    trigger: 'blur',
                  },
                  {
                    pattern: /^1[3456789]\d{9}$/,
                    message: '请输入正确的手机号码',
                    trigger: 'blur',
                  },
                ],
                goodsFileType: [
                  {
                    required: true,
                    message: '请输入类型',
                    trigger: 'blur',
                  },
                ],
              },
            },
          },

          // {
          // 	label: "分配部门",
          // 	state: false,
          // 	dataState: "0",
          // 	id: "edit23",
          // 	color: "red",
          // },

          // {
          // 	label: "分配群组",
          // 	state: false,
          // 	dataState: "0",
          // 	id: "edit24",
          // 	color: "red",
          // },

          {
            label: '移除',
            state: true,
            promptTitle: '是否确定移除！',
            params: {
              employeeId: '',
            },
            id: 'del',
            color: 'red',
            url: '/web/org/employee/deleteEmployee.json',
          },
        ],
      },
      temTab: {
        tabPane: [
          {
            label: "全部",
            name: "dataState",
            value: "",
          },
          {
            label: "启用",
            name: "dataState",
            value: "0",
          },
          {
            label: "禁用",
            name: "dataState",
            value: "-1",
          },
        ],
      },
      selUrl: '/web/org/employee/queryEmployeePage.json?exportFlag=true',

      tableBtnSelectOptions: [],
      dialogTableVisible: false,
      dialogTableVisible1: false,
      dialogVisible2: false,
      dialogTableVisible2: false,
      rowData: {},
      detailData: {},
      activeName: 'first',
      dapartdata: [],
      dapartdata1: [],
      groupdata: [],
      userInforData: [],
      userCodeData: [],
      pageNo1: 1,
      pageSize1: 10,
      Num1: 0,
      pageNo2: 1,
      excelState: 1,
      pageSize2: 10,
      Num3: 0,
      datas: {},
      newPhone: '',
    }
  },
  mounted() {
    searchcompany().then((res) => {
      res.rows.map((el) => {
        this.$set(el, 'value', el.companyCode)
        this.$set(el, 'label', el.companyName)
      })
      this.userInforData = [...res.rows]
      this.companyShortname = [...res.rows]
      this.temTable.tableBtn[1].template.html[0].options = [...res.rows]
    })
    //员工列表查询机构服务分页列表
    queryCompanyPageByYG().then((res) => {
      res.rows.map((el) => {
        this.$set(el, 'value', el.companyCode)
        this.$set(el, 'label', el.companyName)
      })
      //	      this.temInput[4].btnArray[1].template.html[0].options = [...res.rows];
    })
    //员工列表查询用户分页列表
    queryUserservicePageByYG().then((res) => {
      this.userCodeData = res.rows
      let datas = []
      res.rows.map((el, index) => {
        //        	console.log(el.userPhone)
        if (el.userPhone) {
          datas.push(res.rows[index])
        }
        if (el.userPhone) {
          this.$set(el, 'value', el.userCode)
          this.$set(el, 'label', el.userPhone)
        }
      })
      //        this.temInput[4].btnArray[1].template.html[5].options = datas;
    })
    //获取公司名称
    getCompany().then((res) => {
      this.optionsCompany = [...res.list]
      // res.rows.map((el) => {
      // 	this.$set(el, "value", el.companyCode);
      // 	this.$set(el, "label", el.companyName);
      // });
      // this.temInput[0].options = [...res.rows];
    })

    //查询联级别组织
    API.queryCompanyAndDepartPage().then(res => {
      console.log('查询联级别组织', res);
      res.map((item, index) => {
        // Ensure that the index in cascaderOptions exists
        if (!this.cascaderOptions[index]) {
          this.cascaderOptions[index] = {};
        }

        this.cascaderOptions[index].value = {
          value: item.companyCode,
          label: item.companyName,
        }
        this.cascaderOptions[index].label = item.companyName;
        this.cascaderOptions[index].children = this.cascaderOptions[index].children || []; // Initialize children if it doesn't exist

        if (item.departList) {
          item.departList.map((item1, index1) => {
            let child = {
              value: {
                value: item1.departCode,
                label: item1.departName,
              },
              label: item1.departName,
              children: [] // Initialize child children array
            };
            this.cascaderOptions[index].children.push(child);

            // Level 3 (subDepartments under department)
            if (item1.chileOrgDepartReDomainList) {
              item1.chileOrgDepartReDomainList.map((item2, index2) => {
                let child2 = {
                  value: {
                    value: item2.departCode,
                    label: item2.departName,
                  },
                  label: item2.departName,
                  children: [] // Initialize children for level 3
                };
                this.cascaderOptions[index].children[index1].children.push(child2);

                // Level 4 (sub-subDepartments under sub-department)
                if (item2.chileOrgDepartReDomainList) {
                  item2.chileOrgDepartReDomainList.map((item3, index3) => {
                    this.cascaderOptions[index].children[index1].children[index2].children.push({
                      value: {
                        value: item3.departCode,
                        label: item3.departName,
                      },
                      label: item3.departName
                    });
                  });
                }
              });
            }
          });
        }
      });

    })
  },
  watch: {
    excelState() {
      //				console.log(this.excel, 'this.excel')
      this.excelArray = []
      let arrNew = []
      let rePhone = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
      let throws = ''
      this.excel.forEach((el, index) => {
        //      let _array = [];
        let msg = {}
        for (let i in el) {
          if (i.indexOf('*登录手机号') > -1) {
            if (rePhone.test(el[i])) {
              msg.employeePhone = el[i]
            } else {
              throws = el[i]
            }

            // arrNew.push(el[i])
          }
          if (i.indexOf('*员工姓名') > -1) {
            msg.employeeName = el[i]
          }
          if (i.indexOf('*所属公司') > -1) {
            msg.companyShortname = el[i]
          }
          if (i.indexOf('*登录密码') > -1) {
            msg.rechargeMoney = el[i]
          }
        }

        this.excelArray.push(msg)
      })

      // let goods = this.getRepeatNum(arrNew),
      // 	arrs = [];
      // for(let i in goods) {
      // 	let mm = {}
      // 	if(goods[i] != 1) {
      // 		mm['name'] = i
      // 		mm['val'] = goods[i]
      // 		arrs.push(mm)
      // 	}
      // }
      // if(arrs && arrs.length > 0) {
      // 	this.$message.error("本次导入流水号" + arrs[0].name + '存在' + arrs[0].val + '个，导入失败');
      // 	return
      // }
      //			      console.log(this.excelArray,'this.excelArray')
      ///web/org/employee/saveEmployeePlusBatch.json
      console.log(this.excelArray, 'this.excelArray')
      if (throws) {
        this.$message.error('错误的手机号：' + throws + ' 请修改后重试！')
        return
      }
      if (this.excelArray && this.excelArray.length == 0) {
        this.$message.error('您本次未导入员工')
        return
      }
      // return
      saveEmployeePlusBatch(
        querystring.stringify({
          orgEmployeeDomainStr: JSON.stringify(this.excelArray),
        }),
      ).then((res) => {
        if (res.dataObj.length == 0) {
          this.$message.success('导入成功')
          this.$refs.child.getDataList()
        } else {
          let list = []
          res.dataObj.forEach((item) => {
            if (list.length <= 5) {
              list.push(item.employeePhone)
            } else {
              return
            }
          })
          this.$message.error('手机号为' + list.toString() + '的数据异常')
          this.$refs.child.getDataList()
        }
      })
    },
  },
  methods: {
    renderTemp1(h, item) {
      return (
        <div>{Number((item.employeeAddress - item.employeeTel).toFixed(2))}</div>
      )
    },
    renderTemp2(h, item) {
      return (
        <div>{Number(item.employeeAddress)}</div>
      )
    },
    //编辑
    editSure(editForm) {
      let companyShortname = ''
      // let pattern = /^1[3456789]\d{9}$/;
      this.optionsCompany.map((item) => {
        if (item.companyCode == editForm.companyCode) {
          companyShortname = item.companyName
        }
      })
      queryUserserviceCurrency(`userPhone=${editForm.employeePhone}`).then((res) => {
        if (res.total == 0) {
          updateEmployee({
            companyCode: editForm.companyCode,
            employeeName: editForm.employeeName,
            employeePhone: editForm.employeePhone,
            employeeId: editForm.employeeId,
            employeeCode: editForm.employeeCode,
            companyShortname,
          }).then((res) => {
            if (res.success) {
              this.$message.success(res.msg)
              this.editVisible = false
              this.$refs.child.getDataList()
            } else {
              this.$message.error(res.msg)
            }
          })
        } else {
          if (this.newPhone == editForm.employeePhone) {
            updateEmployee({
              companyCode: editForm.companyCode,
              employeeName: editForm.employeeName,
              employeePhone: editForm.employeePhone,
              employeeId: editForm.employeeId,
              employeeCode: editForm.employeeCode,
              companyShortname,
            }).then((res) => {
              if (res.success) {
                this.$message.success(res.msg)
                this.editVisible = false
                this.$refs.child.getDataList()
              } else {
                this.$message.error(res.msg)
              }
            })
          } else {
            this.$message.error('请检查手机号(不能为空且不能重复)！')
            return
          }
        }
      })
    },
    closefenpei() {
      this.alllocationForm.companyPositionCode = ''
      this.alllocationForm.departCode = ''
      this.alllocationForm.departPositionCode = ''
      this.alllocationForm.groupCode = ''
      this.alllocationForm.groupPositionCode = ''
    },
    closeAssignmentStyle2() {
      this.assignmentStyle2 = false
    },
    closeAssignmentStyle3() {
      this.assignmentStyle3 = false
    },
    alllocationSure(alllocationForm) {
      let _datas = { ...alllocationForm }
      console.log(_datas, this.detailData, '=======')
      _datas['userinfoCode'] = this.detailData.userinfoCode
      _datas['employeeId'] = this.detailData.employeeId
      _datas['employeeCode'] = this.detailData.employeeCode
      this.detailData.positionName ? (_datas['positionName'] = this.detailData.positionName) : false
      this.detailData.positionCode ? (_datas['positionCode'] = this.detailData.positionCode) : false
      this.detailData.orgDepartempDomain ? (_datas['departempId'] = this.detailData.orgDepartempDomain.departempId) : false
      this.detailData.orgGroupempDomain ? (_datas['groupempId'] = this.detailData.orgGroupempDomain.groupempId) : false

      updateEmployeeToCompany(querystring.stringify(_datas)).then((rem) => {
        if (rem.success) {
          this.$message({
            message: rem.msg,
            type: 'success',
          })
        } else {
          this.$message.error(rem.msg)
        }
        this.alllocationVisible = false
        this.$refs.child.getDataList()
      })
    },
    sureright() {
      this.dialogTableVisible1 = false
      //console.log(this.value5,'点击我');
      var data = []
      for (var j = 0; j < this.value5.length; j++) {
        data.push(this.value5)
      }
      data = this.options.filter((item) => {
        return this.value5.indexOf(item.departCode) > -1
      })
      this.rowData.orgdepartList = data
      let params = JSON.stringify(this.rowData)
      //    console.log(params);
      let json = { params: params }
      updatedepart(querystring.stringify(json)).then((res) => {
        this.$message({
          duration: 3000,
          message: res.msg,
          type: 'success',
        })
      })
    },
    importfxx(obj) {
      let _this = this
      console.log(obj, '11111')
      let inputDOM = this.$refs.inputer
      // 通过DOM取文件数据

      this.file = event.currentTarget.files[0]
      // console.log(this.file);

      var rABS = false //是否将文件读取为二进制字符串
      var f = this.file

      var reader = new FileReader()
      //if (!FileReader.prototype.readAsBinaryString) {
      FileReader.prototype.readAsBinaryString = (f) => {
        var binary = ''
        var rABS = false //是否将文件读取为二进制字符串
        var pt = this
        var wb //读取完成的数据
        var outdata
        var reader = new FileReader()
        reader.onload = (e) => {
          var bytes = new Uint8Array(reader.result)
          var length = bytes.byteLength
          for (var i = 0; i < length; i++) {
            binary += String.fromCharCode(bytes[i])
          }
          var XLSX = require('xlsx')
          if (rABS) {
            wb = XLSX.read(btoa(fixdata(binary)), {
              //手动转化
              type: 'base64',
            })
          } else {
            wb = XLSX.read(binary, {
              type: 'binary',
            })
          }
          console.log(wb, 'dfg')
          outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]) //outdata就是你想要的东西
          this.excel = outdata
          console.log(this.excel, 'this.excel ')
          this.excelState++
          console.log(this.excelState, 'this.excelState')
        }
        reader.readAsArrayBuffer(f)
      }
      if (rABS) {
        reader.readAsArrayBuffer(f)
      } else {
        reader.readAsBinaryString(f)
      }
    },
    surerightone() {
      this.dialogTableVisible2 = false
      //    console.log(this.value6);
      var data = []
      for (var j = 0; j < this.value6.length; j++) {
        data.push(this.value6)
      }
      data = this.options1.filter((item) => {
        return this.value6.indexOf(item.groupCode) > -1
      })
      this.rowData.orggroupList = data
      let params = JSON.stringify(this.rowData)
      let json = { params: params }
      updategroup(querystring.stringify(json)).then((res) => {
        this.$message({
          duration: 3000,
          message: res.msg,
          type: 'success',
        })
      })
    },
    inputBtnClick(item, index) {
      console.log('其他按钮通用事件')
      if (item.id === 'insert') {
        this.$refs.xlsxFile.value = ''
        this.$refs.xlsxFile.click()
      }
      if (item.id === 'loaddown') {
        location.href = 'https://qjstatic.oss-cn-shanghai.aliyuncs.com/JD/employeeList.xlsx'
      }
    },
    //tab切换
    handleClick(tab, event) {
      console.log(tab, "tab 切换");
      console.log('this.activeName:', this.activeName);

    },
    //table填写数据
    getData(data) {
      this.temTable.tableData = data.map((item) => {
        item.departPositionName = item.orgDepartempDomain ? item.orgDepartempDomain.positionName : ''
        item.groupPositionName = item.orgGroupempDomain ? item.orgGroupempDomain.positionName : ''
      })
    },
    selChange(data, item) {
      //    console.log(data, item, "selchange");
      let obj = {}
      if (item.id === 'userPCode') {
        obj = this.userCodeData.find((item) => {
          return item.userCode == data
        })
        if (obj.userPhone) {
          this.temInput[4].btnArray[1].template.html[4].value = obj.userPhone
        } else {
          this.temInput[4].btnArray[1].template.html[4].value = ''
        }
        this.temInput[4].btnArray[1].template.html[1].value = obj.userPcode
      }
      if (item.id === 'selCode') {
        obj = this.companyShortname.find((item) => {
          return item.companyCode == data
        })
        this.temInput[4].btnArray[1].template.html[2].value = obj.companyShortname
      }
      //console.log(this.temInput[4].btnArray[1].template);
    },
    btnClick() { },
    clearAllgroup() {
      this.alllocationForm.groupPositionCode = ''
    },
    clearAlldepart() {
      this.alllocationForm.departPositionCode = ''
    },
    getEmployees1(val, i, e) {
      if (!e) {
        switch (i) {
          case 1:
            break
          case 2:
            let params1 = {
              companyCode: val.companyCode,
              positionShortcode: val.departCode,
              positionType: 1,
            }
            queryPositionPage(params1).then((res) => {
              this.optionParent3 = [...res.rows]
              if (this.alllocationForm.departCode) {
                this.alllocationForm.departPositionCode = this.optionParent3[0] ? this.optionParent3[0].positionCode : ''
              }
            })
            break
          case 3:
            break
          case 4:
            let params2 = {
              companyCode: val.companyCode,
              positionShortcode: val.groupCode,
              positionType: 2,
            }
            queryPositionPage(params2).then((res) => {
              this.optionParent5 = [...res.rows]
              if (this.alllocationForm.groupCode) {
                this.alllocationForm.groupPositionCode = this.optionParent5[0] ? this.optionParent5[0].positionCode : ''
              }
            })
            break
          default:
            break
        }
        this.$forceUpdate()
      }
    },
    tabBtn(data, item, index) {
      console.log(item)
      this.rowData = data
      this.detailData = data

      if (item.id === 'edit22') {

        this.tableData[0] = data
        this.dapartdata1[0] = data.orgDepartempDomain
        this.groupdata[0] = data.orgGroupempDomain
        this.datas = data
        this.companyCode = data.companyCode
        this.dialogTableVisible = true
        console.log(data, 'data数据')
      }
      if (item.id == 'edit222') {
        this.alllocationVisible = true
        let params = {
          companyCode: data.companyCode,
          positionShortcode: data.companyCode,
          positionType: 0,
        }
        this.alllocationForm.companyCode = data.companyCode
        this.alllocationForm.departCode = data.orgDepartempDomain ? data.orgDepartempDomain.departCode : ''
        this.alllocationForm.groupCode = data.orgGroupempDomain ? data.orgGroupempDomain.groupCode : ''
        queryPositionPage(params).then((res) => {
          this.optionParent1 = [...res.rows]
          if (this.alllocationForm.companyCode) {
            this.alllocationForm.companyPositionCode = this.optionParent1[0] ? this.optionParent1[0].positionCode : ''
            this.alllocationForm.companyPositionCode = data.positionCode || ''
          }
        })
        let params1 = {
          companyCode: data.companyCode,
        }
        searchdepartment(params1).then((res) => {
          this.optionParent2 = [...res.rows]
        })
        if (this.alllocationForm.departCode && data.orgDepartempDomain) {
          let params3 = {
            companyCode: data.companyCode,
            positionShortcode: data.orgDepartempDomain.departCode,
            positionType: 1,
          }
          queryPositionPage(params3).then((res) => {
            this.optionParent3 = [...res.rows]
            this.alllocationForm.departPositionCode = this.optionParent3[0] ? this.optionParent3[0].positionCode : ''
            this.alllocationForm.departPositionCode = data.orgDepartempDomain ? data.orgDepartempDomain.positionCode : ''
          })
        }
        let params2 = {
          companyCode: data.companyCode,
        }
        searchgroup(params2).then((res) => {
          this.optionParent4 = [...res.rows]
        })
        if (this.alllocationForm.groupCode && data.orgGroupempDomain) {
          let params4 = {
            companyCode: data.companyCode,
            positionShortcode: data.orgGroupempDomain.groupCode,
            positionType: 2,
          }
          queryPositionPage(params4).then((res) => {
            this.optionParent5 = [...res.rows]
            this.alllocationForm.groupPositionCode = this.optionParent5[0] ? this.optionParent5[0].positionCode : ''
            this.alllocationForm.groupPositionCode = data.orgGroupempDomain ? data.orgGroupempDomain.positionCode : ''
          })
        }
      }
      if (item.id == 'edit333') {
        this.assignmentStyle2 = true
      }
      if (item.id == 'edit444') {
        this.assignmentStyle3 = true
      }
      if (item.id == 'edit1') {
        this.newPhone = data.employeePhone
        this.editForm = JSON.parse(JSON.stringify(data))
        this.editVisible = true
        console.log(data, 'data数据')
      }
      if (item.id === 'edit23') {
        this.dialogTableVisible1 = true
        let _datas = {
          companyCode: data.companyCode,
          rows: '10000',
          page: '1',
        }
        //员工列表根据公司Code查询部门服务分页列表
        queryDepartPageByYG(_datas).then((res) => {
          //            console.log(res);
          this.options = res.rows
          this.sdpartcode.push(res.rows.departCode)
          let _data = {
            employeeCode: data.employeeCode,
          }
          //查询Code员工详情
          searchmemberdetail(_data).then((res) => {
            //console.log(res, "rrr");
            this.value5 = []
            this.dapartdata = res.departList
            for (var j = 0; j < res.departList.length; j++) {
              // if (this.sdpartcode.includes(res.departList[j].departCode)) {
              this.value5.push(res.departList[j].departCode)
              // }
            }
            //console.log(this.value5)
          })
        })
      }
      if (item.id === 'edit24') {
        //console.log("我点击ss1");
        this.dialogTableVisible2 = true
        let _datas = {
          companyCode: data.companyCode,
          rows: '10000',
          page: '1',
        }
        //员工列表根据公司Code查询群组服务分页列表
        queryGroupPageByYG(_datas).then((res) => {
          //            console.log(res);
          this.options1 = res.rows
          let _data = {
            employeeCode: data.employeeCode,
          }
          //查询Code员工详情
          searchmemberdetail(_data).then((res) => {
            this.value6 = []
            this.dapartdata = res.groupList
            for (var j = 0; j < res.groupList.length; j++) {
              // if (this.zdpartcode.includes(res.groupList[j].groupCode)) {
              this.value6.push(res.groupList[j].groupCode)
              // }
            }
          })
        })
      }
    },
    closed() {
      this.dialogTableVisible = false;
    },
    assignmentStyle2Submit() {
      console.log('最终选择的组织项this.cascaderValue：', this.cascaderValue);
      if (Array.isArray(this.cascaderValue) && this.cascaderValue.length <= 0) {
        this.$message({ type: 'warning', message: '请选择组织!', })
      } else if (Array.isArray(this.multipleSelection) && this.multipleSelection.length <= 0) {
        this.$message({ message: '还没有选择员工哦', type: 'warning' });
      } else {
        console.log('最终选择的组织项this.cascaderValue：', this.cascaderValue);
        console.log('最终选择的员工this.multipleSelection', this.multipleSelection);

        //接下来网络请求提交数据员工列表分配组织


      }
    },
    //联级选择器多选一选择事件
    handleCascaderChange(value) {
      console.log('联级选择器多选一this.cascaderValue:', this.cascaderValue);
      //[{ value: 'companyCode', label: 'companyName', }, {… }, {… }, {… }]
      console.log(value);  //[{ value: 'companyCode', label: 'companyName', }, {… }, {… }, {… }]
    },
    handleSelectionChange(val) {
      //勾选中的行
      // console.log(val, "valqwe");
      this.multipleSelection = val;
    },
    selectRowChange(rows) {
      console.log('树形选择器选中的rows', rows);
      this.selectedRows = rows
    },
    assignmentStyle3Submit() {
      console.log('分配管理组织确定按钮');
      console.log('组织this.selectedRows', this.selectedRows);

      if (this.selectedRows == null || this.selectedRows == undefined) {
        this.$message.error('组织数据类型出错')
      } else if (Array.isArray(this.selectedRows) && this.selectedRows.length != 1) {
        this.$message({ message: '一次只能选择一个组织哦', type: 'warning' });
      } else if (Array.isArray(this.multipleSelection) && this.multipleSelection.length <= 0) {
        this.$message({ message: '还没有选择员工哦', type: 'warning' });
      } else {
        console.log('最终选择的员工this.multipleSelection', this.multipleSelection);
        console.log('最终选择组织this.selectedRows', this.selectedRows);
        //接下来网络请求提交数据员工列表分配组织


      }
    }
  },
}
</script>
